/*******************************************************
PGAD
Authors:
	Rui Eduardo Gouveia Gil

Reviewers: 
	TODO

Version 1
Date: 2009
File: interface.js
 *******************************************************/
function parseXML(xml)
{
	if (window.DOMParser) {
		return new DOMParser().parseFromString(xml,"text/xml");
  	} else {
		var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.loadXML(xml);
  			
		return xmlDoc;
	}	
} 

function verifyIP (ip) {
	var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	var ipArray = ip.match(ipPattern);

	if (ip == "0.0.0.0")
		return false;

	if (ip == "127.0.0.1")
		return false;

	if (ipArray == null)
		return false;
	else {
		for (i = 0; i < 4; i++) {
			thisSegment = ipArray[i];
			if (thisSegment > 255) {
				return false;
				i = 4;
			}
			if ((i == 0) && (thisSegment > 255)) {
				return false;
				i = 4;
	      	}
   		}
	}
	
	return true;
}

function verifyNetmask (netmask) {
	var correct_range = {128:1,192:1,224:1,240:1,248:1,252:1,254:1,255:1,0:1};
	var m = netmask.split('.');

	for (var i = 0; i <= 3; i ++) {
		if (!(m[i] in correct_range)) {
			return false;
		}
	}
	
	if ((m[0] == 0) || (m[0] != 255 && m[1] != 0) || (m[1] != 255 && m[2] != 0) || (m[2] != 255 && m[3] != 0)) {
		return false;
	}
	
	return true;
}

function verifyNumber (value, min, max) {
	
	if((value - 0) == value && value.length > 0)
	{
		if(min && value < min)
			return false;

		if(max && value > max)
			return false;

		return true;		
	}
	else
		return false;	
}

function checkTime(value) 
{ 
	var errorMsg = ""; 
	// regular expression to match required time format 
	re = /^(\d{1,2}):(\d{2})(:00)?([ap]m)?$/; 
	
	if(regs = value.match(re)) { 
		if(regs[4]) { 
			// 12-hour time format with am/pm
			if(regs[1] < 1 || regs[1] > 12) {
				errorMsg = "Invalid value for hours: " + regs[1]; 
			} 
		} else { 
			// 24-hour time format
			if(regs[1] > 23) {
				errorMsg = "Invalid value for hours: " + regs[1]; 
			} 
		} 
			
		if(!errorMsg && regs[2] > 59) {
			errorMsg = "Invalid value for minutes: " + regs[2];
		} 
	} 
	else { 
		errorMsg = "Invalid time format: " + value; 
	} 

	return errorMsg;
}

function executeScript(obj,text)
{  
	var script = new Function(text);
	script();
}

function executeScriptLoading(obj,text)
{  
	executeScript(obj,text);
	$('loading').style.visibility='hidden';
}

function redrawLayer(obj,data)
{
	var i = 0;
	for (i = 0; i+1 < data.length; i += 2)
		$(data[i]).innerHTML = data[i+1];	

	if(data[i])
		executeScript(obj,data[i]);

	$('loading').style.visibility='hidden';
}

function redrawLayerNoLoading(obj,data)
{
	var i = 0;
	for (i = 0 ; i+1 < data.length ; i += 2)
		$(data[i]).innerHTML = data[i+1];	

	if(data[i])
		executeScript(obj,data[i]);
}

function checkEnter(e)
{ 
	if(e && e.which)	//if which property of event object is supported (NN4)
		return (e.which == 13); //character code is contained in NN4's which property
	else
		return (e.keyCode == 13); //character code is contained in IE's keyCode property
}

var tooltip_time;
function showTooltip(current,e,text)
{
	if (browser=navigator.appName == "Microsoft Internet Explorer" && parseFloat(navigator.appVersion) <= 4)
		return;
	else
		tooltip_time = setTimeout(showTooltipNow,1000,current,e,text);
}

function showTooltipNow(current,e,text)
{
	var tooltipElement;
	if (document.getElementById("tooltipElement") == null)
	{
		tooltipElement = document.createElement("div");
		tooltipElement.setAttribute('id','tooltipElement');
		document.body.appendChild(tooltipElement);
	}
	else
	{
		tooltipElement = document.getElementById("tooltipElement");
	}
	
	var posx = -1;
	var posy = -1;
	if (browser=navigator.appName != "Microsoft Internet Explorer")
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	
	if (posx==-1 && posy==-1)
	{
		posx = window.event.clientX + document.documentElement.scrollLeft;
		posy = window.event.clientY + document.documentElement.scrollTop;
	}
	
	tooltipElement.innerHTML = text;
	tooltipElement.style.left=posx+'px';
	tooltipElement.style.top=posy+'px';

	tooltipElement.style.visibility='visible';
}

function hideTooltip()
{
	var tooltipElement = document.getElementById("tooltipElement");
	if (tooltipElement != null)
	{
		tooltipElement.style.visibility='hidden';
		clearTimeout(tooltip_time);
	}
}

